package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.cst.SysConst;
import com.itheima.dto.OrderDto;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.res.OrderRes;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import com.itheima.utils.RedisUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private MemberService memberService;

    @Resource
    private OrderSettingService orderSettingService;

    @Override
    @Transactional
    public Long appoOrder(OrderDto orderDto) {
        //1.短信验证码校验
        String code = RedisUtil.get(SysConst.SMS_VALIFYCODE + orderDto.getTelphone());
        if(code == null){
            throw new RuntimeException("短信验证码过期或者未发送短信验证码。");
        }
        if(code.equals(orderDto.getValidateCode()) == false){
            throw new RuntimeException("验证码错误！");
        }
        String orderDateFormatter = DateUtil.formatDate(orderDto.getOrderDate());
        //2.判断是否重复预约
        Member member = memberService.findMemberByTelphone(orderDto.getTelphone());
        if(member != null){
            Order order = existOrder(member.getId(),orderDateFormatter);
            if(order != null){
                throw new RuntimeException("您已经预约了！");
            }
        }
        //3.选择的预约日期是否可以预约
        OrderSetting orderSetting = orderSettingService.findByDate(orderDateFormatter);
        if(orderSetting == null){
            throw new RuntimeException("该日期不预约");
        }
        if(orderSetting.getOrderNumber() == orderSetting.getOrdered()){
            throw new RuntimeException("该日期已预约满了！");
        }
        //4.预约设置预约数修改
        orderSetting.setOrdered(orderSetting.getOrdered() + 1);
        orderSettingService.updateById(orderSetting);
        //5.会员信息维护
        if(member == null){
            member = new Member();
            member.setCreateDate(new Date());
        }
        member.setTelphone(orderDto.getTelphone());
        member.setIdCard(orderDto.getIdCard());
        member.setName(orderDto.getName());
        member.setSex(orderDto.getSex());
        memberService.saveOrUpdate(member);
        //6.预约订单
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDto.getOrderDate());
        order.setSetmealId(orderDto.getSetmealId());
        this.save(order);
        return order.getId();
    }

    @Override
    public Order existOrder(Long memberId,String orderDate) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("order_date", orderDate);
        return this.getOne(queryWrapper);
    }

    @Override
    public OrderRes getOrderInfo(Long orderId) {
        return this.baseMapper.selectOrderInfo(orderId);
    }

}
